package com.control.qct.businesssteward.activity;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.adapter.EverydayAccountAdapter;
import com.control.qct.businesssteward.adapter.PayItemsAdapter;
import com.control.qct.businesssteward.base.BaseActivity;
import com.control.qct.businesssteward.base.BaseBean;
import com.control.qct.businesssteward.httputils.HttpUtils;
import com.control.qct.businesssteward.model.EverydayAccountModel;
import com.control.qct.businesssteward.fragment.AccountSelectFragment;
import com.control.qct.businesssteward.model.StoreModel;
import com.control.qct.businesssteward.utils.DateUtil;
import com.control.qct.businesssteward.utils.DecimalFormatUtils;
import com.control.qct.businesssteward.utils.ScreenUtil;
import com.control.qct.businesssteward.utils.TextContentUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * 日结月结
 * Created by Administrator on 2017/12/12 0012.
 */

public class EverydayAccountActivity extends BaseActivity implements AccountSelectFragment.ICallbackDateListener {
    @BindView(R.id.tv_all_saleroom_title)
    TextView mTvAllSaleroomTitle;//日结合计或月结合计标题
    @BindView(R.id.tv_all_saleroom)
    TextView mTvAllSaleroom;//合计金额
    @BindView(R.id.tv_all_sale_num)
    TextView mTvAllSaleNum;//总销售笔数
    @BindView(R.id.tv_all_exchange_income)
    TextView mTvAllExchangeIncome;//退换货补价金额
    @BindView(R.id.tv_all_return_goods_outcome)
    TextView mTvAllReturnGoodsOutcome;//退换货退款
    @BindView(R.id.tv_all_return_order_total)
    TextView mTvAllReturnOrderTotal;//总赠品合计

    @BindView(R.id.rv_pay_items)
    RecyclerView mRvPayItems;//店铺日结支付列表
    @BindView(R.id.rv_everyday_account)
    RecyclerView mRvEverydayAccount;//店铺日结列表
    @BindView(R.id.nsv_everyday_account)
    NestedScrollView mNsvEverydayAccount;
    @BindView(R.id.fl_content)
    FrameLayout mFlContent;
    @BindView(R.id.dl_menu)
    public DrawerLayout mDlMenu;//侧滑菜单 -- 选择日结或月结

    private EverydayAccountAdapter mEverydayAccountAdapter;
    ;

    private Map<String, String> mParams;
    private List<EverydayAccountModel.PayitemsBean> mPayitemsBeanList = new ArrayList<>();
    private List<EverydayAccountModel.SummaryBean.StoreSaleOrderDetailsBean> mEverydayAccountModelList = new
            ArrayList<>();
    private PayItemsAdapter mPayItemsAdapter;
    private int mId = 1;
    private String mStoreId = "";
    private AccountSelectFragment mAccountSelectFragment;

    public EverydayAccountActivity() {
        super(R.layout.activity_everyday_account);
    }

    @Override
    protected void initView() {
        setLeftIcon(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("日结");
        setRightIcon("筛选", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDlMenu.openDrawer(mFlContent);//打开侧滑菜单
            }
        });


        //设置侧滑菜单布局
        ViewGroup.LayoutParams params = mFlContent.getLayoutParams();
        params.width = (int) (ScreenUtil.getScreenWidthPix(this) * 0.85);
        mFlContent.setLayoutParams(params);
        mAccountSelectFragment = new AccountSelectFragment(this);
        replaceFragment(R.id.fl_content, mAccountSelectFragment);
        mDlMenu.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                mAccountSelectFragment.mRgAccountSelect.check(mId == 1 ? R.id.rb_day_account : R.id.rb_month_account);
            }
        });
        getDaySummary();
        //支付列表
        mRvPayItems.setLayoutManager(new GridLayoutManager(this, 2));
        mPayItemsAdapter = new PayItemsAdapter(mPayitemsBeanList);
        mRvPayItems.setAdapter(mPayItemsAdapter);
        //门店列表
        mRvEverydayAccount.setLayoutManager(new LinearLayoutManager(this));
        mEverydayAccountAdapter = new EverydayAccountAdapter(mEverydayAccountModelList);
        mRvEverydayAccount.setAdapter(mEverydayAccountAdapter);
        mEverydayAccountAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt("id", mId);
                bundle.putString("storeId", mEverydayAccountModelList.get(position).getStoreId() + "");
                bundle.putString("storeName", mEverydayAccountModelList.get(position).getStoreTitle());
                if (mId == 1) {
                    bundle.putString("date", DateUtil.toTime6(System.currentTimeMillis()));
                } else {
                    bundle.putString("startDate", DateUtil.getFirstDayOfMonth());
                    bundle.putString("endDate", DateUtil.toTime6(System.currentTimeMillis()));
                }
                startActivity(AccountDetailActivity.class, bundle);
            }
        });
    }




    /**
     * 获取日结汇总
     */
    private void getDaySummary() {
        mParams = new HashMap<>();
        mParams.put("date", DateUtil.toTime6(System.currentTimeMillis()));
        mParams.put("storeId", mStoreId);
        mNetProtocol.getDaySummary(this, mParams, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                EverydayAccountModel everydayAccountModel = (EverydayAccountModel) baseBean.getData();
                setData(everydayAccountModel);
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }

    /**
     * 获取月结汇总
     */
    private void getMonthSummary() {
        mParams = new HashMap<>();
        mParams.put("startDate", DateUtil.getFirstDayOfMonth());
        mParams.put("endDate", DateUtil.toTime6(System.currentTimeMillis()));
        mParams.put("storeId", mStoreId);
        mNetProtocol.getMonthSummary(this, mParams, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                EverydayAccountModel everydayAccountModel = (EverydayAccountModel) baseBean.getData();
                setData(everydayAccountModel);
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }

    /**
     * 设置数据
     *
     * @param everydayAccountModel
     */
    private void setData(EverydayAccountModel everydayAccountModel) {
        setTitle(mId == 1 ? "日结" : "月结");
        mTvAllSaleroomTitle.setText(mId == 1 ? "今日总销售合计(元)" : "本月总销售合计(元)");
        //设置部分字体大小--总销售金额统计
        TextContentUtils.getInstance(EverydayAccountActivity.this).setTextSize(mTvAllSaleroom,
                DecimalFormatUtils.DecimalFormatTwo(everydayAccountModel.getSummary().getSsMoney()));
        //总销售笔数
        mTvAllSaleNum.setText("共" + everydayAccountModel.getSummary().getSsCount() + "笔");
        //退换货补价金额
        mTvAllExchangeIncome.setText(DecimalFormatUtils.DecimalFormatTwo(everydayAccountModel.getSummary()
                .getTuiHuanMoney()));
        //退换货退款金额
        mTvAllReturnGoodsOutcome.setText(DecimalFormatUtils.DecimalFormatTwo(everydayAccountModel.getSummary()
                .getTuiMoney()));
        //总赠品合计
        mTvAllReturnOrderTotal.setText(everydayAccountModel.getSummary().getZsCount() + "");

        mPayitemsBeanList.clear();
        mEverydayAccountModelList.clear();
        mPayitemsBeanList.addAll(everydayAccountModel.getPayitems());
        mEverydayAccountModelList.addAll(everydayAccountModel.getSummary().getStoreSaleOrderDetails());
        if (mEverydayAccountAdapter != null) {
            mEverydayAccountAdapter.notifyDataSetChanged();
        }
        if (mPayItemsAdapter != null) {
            mPayItemsAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 重写back键
     */
    @Override
    public void onBackPressed() {
        if (mDlMenu.isDrawerOpen(mFlContent)) {
            mDlMenu.closeDrawers();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void callbackDateId(int id, String storeId) {
        mStoreId = storeId;
        mId = id;
        switch (id) {
            case 1:
                getDaySummary();
                break;
            default:
                getMonthSummary();
                break;
        }
    }
}
